<?php
namespace Api\Controller;

use Common\Model\UserCardListModel;
use Common\Util\CmsData;

class SquareController extends AuthController{

	//	获取名片搜索热词
	public function getCardHot(){
		$user_info = $this->checkUserUtoken();
		$card_hot = ['消费返现'];
		$this->return_app_data(1, $card_hot);
	}

	//	获取文章列表
	public function getCardList(){
		$user_info = $this->checkUserUtoken();
		$search_keyword = $_POST['search_keyword'] ? trim($_POST['search_keyword']) : '';
		$page_size = $_POST['page_size'] ? intval($_POST['page_size']) : 0;
		$page_limit = $_POST['page_limit'] ? intval($_POST['page_limit']) : 0;
		$lat = $_POST['lat'] ? trim($_POST['lat']) : 0;
		$lng = $_POST['lng'] ? trim($_POST['lng']) : 0;
		$map = [
            'square_is_show' => 1
        ];
		if(!empty($search_keyword)){
			$map['_string'] = "FIND_IN_SET('{$search_keyword}',zhiye_label)";
		}
		$user_card = new UserCardListModel();
		$card_list = $user_card->getCardList($map, 'juli_number asc', $page_size, $page_limit, $user_info['id'], $lat, $lng);
		$this->return_app_data(1, $card_list);
	}

	//	名片收藏操作
	public function cardCollectAction(){
		$user_info = $this->checkUserUtoken();
		$card_id = $_POST['card_id'] ? intval($_POST['card_id']) : 0;
		//		判断是否收藏
		$user_card_list_collect = M("user_card_list_collect");
		$collect = $user_card_list_collect->where(['user_id' => $user_info['id'], 'card_id' => $card_id])->find();
		if($collect){
			$action = $user_card_list_collect->where(['id' => $collect['id']])->delete();
		}else{
			$action = $user_card_list_collect->add([
				'user_id'     => $user_info['id'],
				'card_id'     => $card_id,
				'add_time'    => time(),
				'update_time' => time(),
			]);
		}
		if($action){
			$this->return_app_data(1, '收藏成功');
		}else{
			$this->return_app_data(0, '收藏失败');
		}
	}

	public function getCardInfo(){
		$user_info = $this->checkUserUtoken();
		$card_m = new UserCardListModel();
		$card_id = $_POST['card_id'] ? intval($_POST['card_id']) : 0;
		$card_info = $card_m->where(['id' => $card_id])->find();
		$data['card_info'] = $card_info ? $card_info : [];
		$this->return_app_data(1, $data);
	}
}
